import get from '../get'
import Utils from "@/common/Utils";
export default (_fileKey, _uploadPath, ossDir, authRes, type = 'file') => {
	console.log(_uploadPath)

	return new Promise((resolve, reject) => {
		let formData = { //下面的属性名称 大小写无所谓 都能通过
			'Key': `${ossDir}/${_fileKey}`, //OSS存储路径/图片名称 => 如：task/test.jpg
			'Policy': authRes.Policy, //后端Base64编码格式的授权策略 用于签名
			'OSSAccessKeyId': authRes.AccessKeyId, //OSS控制台获取
			'Success_Action_Status': '200', //让服务端返回200,不然，成功上传图片后阿里默认返回statusCode: 204
			'Signature': authRes.Signature, //服务端返回的签名内容 
		}
		var params = {}
		if (type == 'file') {
			params = {
				url: authRes.Host,
				file: _uploadPath, //choostimage 返回的临时图片路径
				fileType: 'image',
				name: 'file',
				formData: formData
			}
		} else {
			params = {
				url: authRes.Host,
				filePath: _uploadPath, //choostimage 返回的临时图片路径
				fileType: 'image',
				name: 'file',
				formData: formData
			}
		}



		let uploadTask = uni.uploadFile({
			...params,
			success: (res) => {
				console.log(res);

				res.data = {
					url: `${authRes.Host}/${ossDir}/${_fileKey}`
				};
				resolve(res);
			},
			fail: (err) => {
				console.log('uploadImage fail', err);
				reject(err);
			}
		});

	});
}